// file name: stack.hpp
// description: a stack class, a data structure
// last editor: sunist chan
// macOS Catalina 10.15.5 with Visual Studio Code and GCC11

#include <iostream>

class node
{
    public:
    int innerData;
    node* nextnode;

    node()
    {
        innerData = 0;
        nextnode = NULL;
    }
};

class stack
{
    private:
    node* head;

    node* last()
    {
        return at(length - 1);
    }

    node* at(int index)
    {
        node* operationPointer = head;
        for (int i = 0; i <= index; i++)
        {
            operationPointer = operationPointer -> nextnode;
        }
        return operationPointer;
    }

    public:
    int length;

    stack()
    {
        head = (node*)malloc(sizeof(node));
        length = 0;
    }

    bool empty()
    {
        if (length == 0)
        {
            return true;
        }
        return false;
    }

    void push(int num)
    {
        node *AddNode = new node;
        AddNode -> innerData = num;
        AddNode -> nextnode = NULL;
        last() -> nextnode = AddNode;
        length += 1;
    }

    void push(node num)
    {
        node* AddNode = new node;
        AddNode -> innerData = num.innerData;
        AddNode -> nextnode = NULL;
        last() -> nextnode = AddNode;
        length += 1;
    }

    void pop()
    {
        free(last());
        at(length-1) -> nextnode = NULL;
        length -= 1;
    }

    int top()
    {
        return last() -> innerData;
    }
};